//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration.Design;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// Represents the design manager for the data query.
	/// </summary>
	public sealed class DataQueryConfigurationDesignManager : ConfigurationDesignManager
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="DataQueryConfigurationDesignManager"/> class.
		/// </summary>
		public DataQueryConfigurationDesignManager()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Register the commands and node maps needed for the design manager into the design time.
		/// </summary>
		/// <param name="serviceProvider">The mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
    public override void Register(IServiceProvider serviceProvider)
    {
			DataQueryNodeMapRegistrar nodeMapRegistrar = new DataQueryNodeMapRegistrar(serviceProvider);
			nodeMapRegistrar.Register();
			DataQueryCommandRegistrar commandRegistrar = new DataQueryCommandRegistrar(serviceProvider);
      commandRegistrar.Register();
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Initializes the data query and adds it to the query settings.
		/// </summary>
		/// <param name="serviceProvider">The mechanism for retrieving a service object; that is an object that provides custom support to other objects.</param>
		/// <param name="rootNode">The root node of the application.</param>
		/// <param name="section">The <see cref="ConfigurationSection"/> that was opened from the <see cref="IConfigurationSource"/>.</param>
		protected override void OpenCore(IServiceProvider serviceProvider, ConfigurationApplicationNode rootNode, ConfigurationSection section)
		{
			// match connection string names with conection string nodes
			IConfigurationUIHierarchy hierarchy = ServiceHelper.GetCurrentHierarchy(serviceProvider);
			foreach (DataQueryNode dataQueryNode in hierarchy.FindNodesByType(typeof(DataQueryNode)))
			{
				foreach (ConnectionStringSettingsNode connectionNode in hierarchy.FindNodesByType(typeof(ConnectionStringSettingsNode)))
				{
					if (connectionNode.Name == dataQueryNode.connectionStringName)
					{
						dataQueryNode.DatabaseInstance = connectionNode;
						break;
					}
				}
			}			
		}

		/// <summary>
		/// Gets a <see cref="ConfigurationSectionInfo"/> object containing the Query Block's configuration information.
		/// </summary>
		/// <param name="serviceProvider">A mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		/// <returns>
		/// A <see cref="ConfigurationSectionInfo"/> object containing the Query Block's configuration information.
		/// </returns>
		protected override ConfigurationSectionInfo GetConfigurationSectionInfo(IServiceProvider serviceProvider)
		{
			ConfigurationNode rootNode = ServiceHelper.GetCurrentRootNode(serviceProvider);
			QuerySettingsNode node = null;
			if (rootNode != null)
				node = (QuerySettingsNode)rootNode.Hierarchy.FindNodeByType(rootNode, typeof(QuerySettingsNode));
			QuerySettings querySection = null;
			if (node == null)
				querySection = null;
			else
			{
				QuerySettingsBuilder builder = new QuerySettingsBuilder(serviceProvider, node);
				querySection = builder.Build();
			}
			string protectionProviderName = GetProtectionProviderName(node);

			return new ConfigurationSectionInfo(node, querySection, QuerySettings.SectionName, protectionProviderName);
		}
		#endregion
	}
}
